package com.gitee.wsl.time

import com.gitee.wsl.ext.CASE_INSENSITIVE
import com.gitee.wsl.ext.compile
import com.gitee.wsl.ext.matcher
import com.gitee.wsl.ext.math.addExact
import com.gitee.wsl.ext.math.multiplyExact
import com.gitee.wsl.ext.math.subtractExact
import com.gitee.wsl.ext.math.toIntExact
import com.gitee.wsl.math.Math
import com.gitee.wsl.time.exception.DateTimeException
import com.gitee.wsl.time.exception.DateTimeParseException
import com.gitee.wsl.time.exception.UnsupportedTemporalTypeException
import kotlinx.datetime.LocalDate

/**
 * A date-based amount of time in the ISO-8601 calendar system,
 * such as '2 years, 3 months and 4 days'.
 *
 *
 * This class models a quantity or amount of time in terms of years, months and days.
 * See [Duration] for the time-based equivalent to this class.
 *
 *
 * Durations and periods differ in their treatment of daylight savings time
 * when added to [ZonedDateTime]. A `Duration` will add an exact
 * number of seconds, thus a duration of one day is always exactly 24 hours.
 * By contrast, a `Period` will add a conceptual day, trying to maintain
 * the local time.
 *
 *
 * For example, consider adding a period of one day and a duration of one day to
 * 18:00 on the evening before a daylight savings gap. The `Period` will add
 * the conceptual day and result in a `ZonedDateTime` at 18:00 the following day.
 * By contrast, the `Duration` will add exactly 24 hours, resulting in a
 * `ZonedDateTime` at 19:00 the following day (assuming a one hour DST gap).
 *
 *
 * The supported units of a period are [YEARS][ChronoUnit.YEARS],
 * [MONTHS][ChronoUnit.MONTHS] and [DAYS][ChronoUnit.DAYS].
 * All three fields are always present, but may be set to zero.
 *
 *
 * The ISO-8601 calendar system is the modern civil calendar system used today
 * in most of the world. It is equivalent to the proleptic Gregorian calendar
 * system, in which today's rules for leap years are applied for all time.
 *
 *
 * The period is modeled as a directed amount of time, meaning that individual parts of the
 * period may be negative.
 *
 *
 * This is a [value-based]({@docRoot}/java.base/java/lang/doc-files/ValueBased.html)
 * class; programmers should treat instances that are
 * [equal][.equals] as interchangeable and should not
 * use instances for synchronization, or unpredictable behavior may
 * occur. For example, in a future release, synchronization may fail.
 * The `equals` method should be used for comparisons.
 *
 * @implSpec
 * This class is immutable and thread-safe.
 *
 * @since 1.8
 */
class Period private constructor(
    /**
     * The number of years.
     */
    val years: Int,
    /**
     * The number of months.
     */
    val months: Int,
    /**
     * The number of days.
     */
    val days: Int,
){
    /**
     * Gets the amount of years of this period.
     *
     *
     * This returns the years unit.
     *
     *
     * The months unit is not automatically normalized with the years unit.
     * This means that a period of "15 months" is different to a period
     * of "1 year and 3 months".
     *
     * @return the amount of years of this period, may be negative
     */
    /**
     * Gets the amount of months of this period.
     *
     *
     * This returns the months unit.
     *
     *
     * The months unit is not automatically normalized with the years unit.
     * This means that a period of "15 months" is different to a period
     * of "1 year and 3 months".
     *
     * @return the amount of months of this period, may be negative
     */
    /**
     * Gets the amount of days of this period.
     *
     *
     * This returns the days unit.
     *
     * @return the amount of days of this period, may be negative
     */

    //-----------------------------------------------------------------------
    /**
     * Gets the value of the requested unit.
     *
     *
     * This returns a value for each of the three supported units,
     * [YEARS][ChronoUnit.YEARS], [MONTHS][ChronoUnit.MONTHS] and
     * [DAYS][ChronoUnit.DAYS].
     * All other units throw an exception.
     *
     * @param unit the `TemporalUnit` for which to return the value
     * @return the long value of the unit
     * @throws DateTimeException if the unit is not supported
     * @throws UnsupportedTemporalTypeException if the unit is not supported
     */
    fun get(unit: ChronoUnit): Long {
        return when (unit) {
            ChronoUnit.YEARS -> {
                this.years.toLong()
            }
            ChronoUnit.MONTHS -> {
                this.months.toLong()
            }
            ChronoUnit.DAYS -> {
                this.days.toLong()
            }
            else -> {
                throw UnsupportedTemporalTypeException("Unsupported unit: $unit")
            }
        }
    }

    val units: List<ChronoUnit>
        /**
         * Gets the set of units supported by this period.
         *
         *
         * The supported units are [YEARS][ChronoUnit.YEARS],
         * [MONTHS][ChronoUnit.MONTHS] and [DAYS][ChronoUnit.DAYS].
         * They are returned in the order years, months, days.
         *
         *
         * This set can be used in conjunction with [.get]
         * to access the entire state of the period.
         *
         * @return a list containing the years, months and days units, not null
         */
        get() = SUPPORTED_UNITS

    //val chronology: java.time.chrono.IsoChronology
        /**
         * Gets the chronology of this period, which is the ISO calendar system.
         *
         *
         * The `Chronology` represents the calendar system in use.
         * The ISO-8601 calendar system is the modern civil calendar system used today
         * in most of the world. It is equivalent to the proleptic Gregorian calendar
         * system, in which today's rules for leap years are applied for all time.
         *
         * @return the ISO chronology, not null
         */
        //get() = java.time.chrono.IsoChronology.INSTANCE

    //-----------------------------------------------------------------------
    val isZero: Boolean
        /**
         * Checks if all three units of this period are zero.
         *
         *
         * A zero period has the value zero for the years, months and days units.
         *
         * @return true if this period is zero-length
         */
        get() = (this === ZERO)

    val isNegative: Boolean
        /**
         * Checks if any of the three units of this period are negative.
         *
         *
         * This checks whether the years, months or days units are less than zero.
         *
         * @return true if any unit of this period is negative
         */
        get() = years < 0 || months < 0 || days < 0

    //-----------------------------------------------------------------------

    //-----------------------------------------------------------------------
    /**
     * Returns a copy of this period with the specified amount of years.
     *
     *
     * This sets the amount of the years unit in a copy of this period.
     * The months and days units are unaffected.
     *
     *
     * The months unit is not automatically normalized with the years unit.
     * This means that a period of "15 months" is different to a period
     * of "1 year and 3 months".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param years  the years to represent, may be negative
     * @return a `Period` based on this period with the requested years, not null
     */
    fun withYears(years: Int): Period {
        if (years == this.years) {
            return this
        }
        return create(years, months, days)
    }

    /**
     * Returns a copy of this period with the specified amount of months.
     *
     *
     * This sets the amount of the months unit in a copy of this period.
     * The years and days units are unaffected.
     *
     *
     * The months unit is not automatically normalized with the years unit.
     * This means that a period of "15 months" is different to a period
     * of "1 year and 3 months".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param months  the months to represent, may be negative
     * @return a `Period` based on this period with the requested months, not null
     */
    fun withMonths(months: Int): Period {
        if (months == this.months) {
            return this
        }
        return create(years, months, days)
    }

    /**
     * Returns a copy of this period with the specified amount of days.
     *
     *
     * This sets the amount of the days unit in a copy of this period.
     * The years and months units are unaffected.
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param days  the days to represent, may be negative
     * @return a `Period` based on this period with the requested days, not null
     */
    fun withDays(days: Int): Period {
        if (days == this.days) {
            return this
        }
        return create(years, months, days)
    }

    //-----------------------------------------------------------------------
    /**
     * Returns a copy of this period with the specified period added.
     *
     *
     * This operates separately on the years, months and days.
     * No normalization is performed.
     *
     *
     * For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days"
     * returns "3 years, 8 months and 5 days".
     *
     *
     * The specified amount is typically an instance of `Period`.
     * Other types are interpreted using [Period.from].
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param amountToAdd  the amount to add, not null
     * @return a `Period` based on this period with the requested period added, not null
     * @throws DateTimeException if the specified amount has a non-ISO chronology or
     * contains an invalid unit
     * @throws ArithmeticException if numeric overflow occurs
     */
     fun plus(amountToAdd: Period): Period {
        val isoAmount = from(amountToAdd)
        return create(
            Math.addExact(years, isoAmount.years),
            Math.addExact(months, isoAmount.months),
            Math.addExact(days, isoAmount.days)
        )
    }

    /**
     * Returns a copy of this period with the specified years added.
     *
     *
     * This adds the amount to the years unit in a copy of this period.
     * The months and days units are unaffected.
     * For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param yearsToAdd  the years to add, positive or negative
     * @return a `Period` based on this period with the specified years added, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun plusYears(yearsToAdd: Long): Period {
        if (yearsToAdd == 0L) {
            return this
        }
        return create(Math.toIntExact(Math.addExact(years.toLong(), yearsToAdd)), months, days)
    }

    /**
     * Returns a copy of this period with the specified months added.
     *
     *
     * This adds the amount to the months unit in a copy of this period.
     * The years and days units are unaffected.
     * For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8 months and 3 days".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param monthsToAdd  the months to add, positive or negative
     * @return a `Period` based on this period with the specified months added, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun plusMonths(monthsToAdd: Long): Period {
        if (monthsToAdd == 0L) {
            return this
        }
        return create(years, Math.toIntExact(Math.addExact(months.toLong(), monthsToAdd)), days)
    }

    /**
     * Returns a copy of this period with the specified days added.
     *
     *
     * This adds the amount to the days unit in a copy of this period.
     * The years and months units are unaffected.
     * For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param daysToAdd  the days to add, positive or negative
     * @return a `Period` based on this period with the specified days added, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun plusDays(daysToAdd: Long): Period {
        if (daysToAdd == 0L) {
            return this
        }
        return create(years, months, Math.toIntExact(Math.addExact(days.toLong(), daysToAdd)))
    }

    //-----------------------------------------------------------------------
    /**
     * Returns a copy of this period with the specified period subtracted.
     *
     *
     * This operates separately on the years, months and days.
     * No normalization is performed.
     *
     *
     * For example, "1 year, 6 months and 3 days" minus "2 years, 2 months and 2 days"
     * returns "-1 years, 4 months and 1 day".
     *
     *
     * The specified amount is typically an instance of `Period`.
     * Other types are interpreted using [Period.from].
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param amountToSubtract  the amount to subtract, not null
     * @return a `Period` based on this period with the requested period subtracted, not null
     * @throws DateTimeException if the specified amount has a non-ISO chronology or
     * contains an invalid unit
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun minus(amountToSubtract: Period): Period {
        val isoAmount = from(amountToSubtract)
        return create(
            Math.subtractExact(years, isoAmount.years),
            Math.subtractExact(months, isoAmount.months),
            Math.subtractExact(days, isoAmount.days)
        )
    }

    /**
     * Returns a copy of this period with the specified years subtracted.
     *
     *
     * This subtracts the amount from the years unit in a copy of this period.
     * The months and days units are unaffected.
     * For example, "1 year, 6 months and 3 days" minus 2 years returns "-1 years, 6 months and 3 days".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param yearsToSubtract  the years to subtract, positive or negative
     * @return a `Period` based on this period with the specified years subtracted, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun minusYears(yearsToSubtract: Long): Period {
        return (
                if (yearsToSubtract == Long.MIN_VALUE) 
                     plusYears(Long.MAX_VALUE).plusYears(1) 
                else 
                    plusYears(-yearsToSubtract)
                )
    }

    /**
     * Returns a copy of this period with the specified months subtracted.
     *
     *
     * This subtracts the amount from the months unit in a copy of this period.
     * The years and days units are unaffected.
     * For example, "1 year, 6 months and 3 days" minus 2 months returns "1 year, 4 months and 3 days".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param monthsToSubtract  the years to subtract, positive or negative
     * @return a `Period` based on this period with the specified months subtracted, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun minusMonths(monthsToSubtract: Long): Period {
        return (
                if (monthsToSubtract == Long.Companion.MIN_VALUE) 
                    plusMonths(Long.Companion.MAX_VALUE).plusMonths(1) 
                else 
                    plusMonths(-monthsToSubtract)
                )
    }

    /**
     * Returns a copy of this period with the specified days subtracted.
     *
     *
     * This subtracts the amount from the days unit in a copy of this period.
     * The years and months units are unaffected.
     * For example, "1 year, 6 months and 3 days" minus 2 days returns "1 year, 6 months and 1 day".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param daysToSubtract  the months to subtract, positive or negative
     * @return a `Period` based on this period with the specified days subtracted, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
    fun minusDays(daysToSubtract: Long): Period {
        return (
                if (daysToSubtract == Long.MIN_VALUE)
                   plusDays(Long.MAX_VALUE).plusDays(1) 
                else plusDays(-daysToSubtract)
                )
    }

    //-----------------------------------------------------------------------
    /**
     * Returns a new instance with each element in this period multiplied
     * by the specified scalar.
     *
     *
     * This returns a period with each of the years, months and days units
     * individually multiplied.
     * For example, a period of "2 years, -3 months and 4 days" multiplied by
     * 3 will return "6 years, -9 months and 12 days".
     * No normalization is performed.
     *
     * @param scalar  the scalar to multiply by, not null
     * @return a `Period` based on this period with the amounts multiplied by the scalar, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
     fun multipliedBy(scalar: Int): Period {
        if (this === ZERO || scalar == 1) {
            return this
        }
        return create(
            Math.multiplyExact(years, scalar),
            Math.multiplyExact(months, scalar),
            Math.multiplyExact(days, scalar)
        )
    }

    /**
     * Returns a new instance with each amount in this period negated.
     *
     *
     * This returns a period with each of the years, months and days units
     * individually negated.
     * For example, a period of "2 years, -3 months and 4 days" will be
     * negated to "-2 years, 3 months and -4 days".
     * No normalization is performed.
     *
     * @return a `Period` based on this period with the amounts negated, not null
     * @throws ArithmeticException if numeric overflow occurs, which only happens if
     * one of the units has the value `Integer.MIN_VALUE`
     */
    fun negated(): Period {
        return multipliedBy(-1)
    }

    //-----------------------------------------------------------------------
    /**
     * Returns a copy of this period with the years and months normalized.
     *
     *
     * This normalizes the years and months units, leaving the days unit unchanged.
     * The months unit is adjusted to have an absolute value less than 12,
     * with the years unit being adjusted to compensate. For example, a period of
     * "1 Year and 15 months" will be normalized to "2 years and 3 months".
     *
     *
     * The sign of the years and months units will be the same after normalization.
     * For example, a period of "1 year and -25 months" will be normalized to
     * "-1 year and -1 month".
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @return a `Period` based on this period with excess months normalized to years, not null
     * @throws ArithmeticException if numeric overflow occurs
     */
     fun normalized(): Period {
        val totalMonths = toTotalMonths()
        val splitYears = totalMonths / 12
        val splitMonths = (totalMonths % 12).toInt() // no overflow
        if (splitYears == years.toLong() && splitMonths == months) {
            return this
        }
        return create(Math.toIntExact(splitYears), splitMonths, days)
    }

    /**
     * Gets the total number of months in this period.
     *
     *
     * This returns the total number of months in the period by multiplying the
     * number of years by 12 and adding the number of months.
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @return the total number of months in the period, may be negative
     */
    fun toTotalMonths(): Long {
        return years * 12L + months // no overflow
    }

    //-------------------------------------------------------------------------
    /**
     * Adds this period to the specified temporal object.
     *
     *
     * This returns a temporal object of the same observable type as the input
     * with this period added.
     * If the temporal has a chronology, it must be the ISO chronology.
     *
     *
     * In most cases, it is clearer to reverse the calling pattern by using
     * [Temporal.plus].
     * <pre>
     * // these two lines are equivalent, but the second approach is recommended
     * dateTime = thisPeriod.addTo(dateTime);
     * dateTime = dateTime.plus(thisPeriod);
    </pre> *
     *
     *
     * The calculation operates as follows.
     * First, the chronology of the temporal is checked to ensure it is ISO chronology or null.
     * Second, if the months are zero, the years are added if non-zero, otherwise
     * the combination of years and months is added if non-zero.
     * Finally, any days are added.
     *
     *
     * This approach ensures that a partial period can be added to a partial date.
     * For example, a period of years and/or months can be added to a `YearMonth`,
     * but a period including days cannot.
     * The approach also adds years and months together when necessary, which ensures
     * correct behaviour at the end of the month.
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param temporal  the temporal object to adjust, not null
     * @return an object of the same type with the adjustment made, not null
     * @throws DateTimeException if unable to add
     * @throws ArithmeticException if numeric overflow occurs
     */
//     fun addTo(temporal: Temporal): Temporal {
//        var temporal: Temporal = temporal
//        validateChrono(temporal)
//        if (months == 0) {
//            if (years != 0) {
//                temporal = temporal.plus(years.toLong(), ChronoUnit.YEARS)
//            }
//        } else {
//            val totalMonths = toTotalMonths()
//            if (totalMonths != 0L) {
//                temporal = temporal.plus(totalMonths, ChronoUnit.MONTHS)
//            }
//        }
//        if (days != 0) {
//            temporal = temporal.plus(days.toLong(), ChronoUnit.DAYS)
//        }
//        return temporal
//    }

    /**
     * Subtracts this period from the specified temporal object.
     *
     *
     * This returns a temporal object of the same observable type as the input
     * with this period subtracted.
     * If the temporal has a chronology, it must be the ISO chronology.
     *
     *
     * In most cases, it is clearer to reverse the calling pattern by using
     * [Temporal.minus].
     * <pre>
     * // these two lines are equivalent, but the second approach is recommended
     * dateTime = thisPeriod.subtractFrom(dateTime);
     * dateTime = dateTime.minus(thisPeriod);
    </pre> *
     *
     *
     * The calculation operates as follows.
     * First, the chronology of the temporal is checked to ensure it is ISO chronology or null.
     * Second, if the months are zero, the years are subtracted if non-zero, otherwise
     * the combination of years and months is subtracted if non-zero.
     * Finally, any days are subtracted.
     *
     *
     * This approach ensures that a partial period can be subtracted from a partial date.
     * For example, a period of years and/or months can be subtracted from a `YearMonth`,
     * but a period including days cannot.
     * The approach also subtracts years and months together when necessary, which ensures
     * correct behaviour at the end of the month.
     *
     *
     * This instance is immutable and unaffected by this method call.
     *
     * @param temporal  the temporal object to adjust, not null
     * @return an object of the same type with the adjustment made, not null
     * @throws DateTimeException if unable to subtract
     * @throws ArithmeticException if numeric overflow occurs
     */
//     fun subtractFrom(temporal: Temporal): Temporal {
//        var temporal: Temporal = temporal
//        validateChrono(temporal)
//        if (months == 0) {
//            if (years != 0) {
//                temporal = temporal.minus(years.toLong(), ChronoUnit.YEARS)
//            }
//        } else {
//            val totalMonths = toTotalMonths()
//            if (totalMonths != 0L) {
//                temporal = temporal.minus(totalMonths, ChronoUnit.MONTHS)
//            }
//        }
//        if (days != 0) {
//            temporal = temporal.minus(days.toLong(), ChronoUnit.DAYS)
//        }
//        return temporal
//    }

    /**
     * Validates that the temporal has the correct chronology.
     */
//    private fun validateChrono(temporal: TemporalAccessor?) {
//        java.util.Objects.requireNonNull<TemporalAccessor?>(temporal, "temporal")
//        val temporalChrono: java.time.chrono.Chronology? =
//            temporal.query<java.time.chrono.Chronology?>(TemporalQueries.chronology())
//        if (temporalChrono != null && java.time.chrono.IsoChronology.INSTANCE == temporalChrono == false) {
//            throw java.time.DateTimeException("Chronology mismatch, expected: ISO, actual: " + temporalChrono.getId())
//        }
//    }

    //-----------------------------------------------------------------------
    /**
     * Checks if this period is equal to another period.
     *
     *
     * The comparison is based on the type `Period` and each of the three amounts.
     * To be equal, the years, months and days units must be individually equal.
     * Note that this means that a period of "15 Months" is not equal to a period
     * of "1 Year and 3 Months".
     *
     * @param other  the object to check, null returns false
     * @return true if this is equal to the other period
     */
    override fun equals(other: Any?): Boolean {
        if (this === other) {
            return true
        }
        return (other is Period)
                && years == other.years && months == other.months && days == other.days
    }

    /**
     * A hash code for this period.
     *
     * @return a suitable hash code
     */
    override fun hashCode(): Int {
        return years + months.rotateLeft(8) + days.rotateLeft(16)
    }

    //-----------------------------------------------------------------------
    /**
     * Outputs this period as a `String`, such as `P6Y3M1D`.
     *
     *
     * The output will be in the ISO-8601 period format.
     * A zero period will be represented as zero days, 'P0D'.
     *
     * @return a string representation of this period, not null
     */
    override fun toString(): String {
        if (this === ZERO) {
            return "P0D"
        } else {
            val buf: StringBuilder = StringBuilder()
            buf.append('P')
            if (years != 0) {
                buf.append(years).append('Y')
            }
            if (months != 0) {
                buf.append(months).append('M')
            }
            if (days != 0) {
                buf.append(days).append('D')
            }
            return buf.toString()
        }
    }

    //-----------------------------------------------------------------------
    /**
     * Writes the object using a
     * [dedicated serialized form]({@docRoot}/serialized-form.html#java.time.Ser).
     * @serialData
     * <pre>
     * out.writeByte(14);  // identifies a Period
     * out.writeInt(years);
     * out.writeInt(months);
     * out.writeInt(days);
    </pre> *
     *
     * @return the instance of `Ser`, not null
     */
//    @java.io.Serial
//    private fun writeReplace(): Any {
//        return java.time.Ser(java.time.Ser.PERIOD_TYPE, this)
//    }

    /**
     * Defend against malicious streams.
     *
     * @param s the stream to read
     * @throws java.io.InvalidObjectException always
     */
//    @java.io.Serial
//    @Throws(java.io.InvalidObjectException::class)
//    private fun readObject(s: java.io.ObjectInputStream?) {
//        throw java.io.InvalidObjectException("Deserialization via serialization delegate")
//    }

//    @Throws(java.io.IOException::class)
//    fun writeExternal(out: java.io.DataOutput) {
//        out.writeInt(years)
//        out.writeInt(months)
//        out.writeInt(days)
//    }

    companion object {
        /**
         * A constant for a period of zero.
         */
        val ZERO: Period = Period(0, 0, 0)

        /**
         * The pattern for parsing.
         */
        private val PATTERN = Regex.compile(
            "([-+]?)P(?:([-+]?[0-9]+)Y)?(?:([-+]?[0-9]+)M)?(?:([-+]?[0-9]+)W)?(?:([-+]?[0-9]+)D)?",
            Regex.CASE_INSENSITIVE
        )

        /**
         * The set of supported units.
         */
        private val SUPPORTED_UNITS: List<ChronoUnit> =
            listOf(
                ChronoUnit.YEARS,
                ChronoUnit.MONTHS,
                ChronoUnit.DAYS
            )

        //-----------------------------------------------------------------------
        /**
         * Obtains a `Period` representing a number of years.
         *
         *
         * The resulting period will have the specified years.
         * The months and days units will be zero.
         *
         * @param years  the number of years, positive or negative
         * @return the period of years, not null
         */
        fun ofYears(years: Int): Period {
            return create(years, 0, 0)
        }

        /**
         * Obtains a `Period` representing a number of months.
         *
         *
         * The resulting period will have the specified months.
         * The years and days units will be zero.
         *
         * @param months  the number of months, positive or negative
         * @return the period of months, not null
         */
        fun ofMonths(months: Int): Period {
            return create(0, months, 0)
        }

        /**
         * Obtains a `Period` representing a number of weeks.
         *
         *
         * The resulting period will be day-based, with the amount of days
         * equal to the number of weeks multiplied by 7.
         * The years and months units will be zero.
         *
         * @param weeks  the number of weeks, positive or negative
         * @return the period, with the input weeks converted to days, not null
         */
        fun ofWeeks(weeks: Int): Period {
            return create(0, 0, Math.multiplyExact(weeks, 7))
        }

        /**
         * Obtains a `Period` representing a number of days.
         *
         *
         * The resulting period will have the specified days.
         * The years and months units will be zero.
         *
         * @param days  the number of days, positive or negative
         * @return the period of days, not null
         */
        fun ofDays(days: Int): Period {
            return create(0, 0, days)
        }

        //-----------------------------------------------------------------------
        /**
         * Obtains a `Period` representing a number of years, months and days.
         *
         *
         * This creates an instance based on years, months and days.
         *
         * @param years  the amount of years, may be negative
         * @param months  the amount of months, may be negative
         * @param days  the amount of days, may be negative
         * @return the period of years, months and days, not null
         */
        fun of(years: Int, months: Int, days: Int): Period {
            return create(years, months, days)
        }

        //-----------------------------------------------------------------------
        /**
         * Obtains an instance of `Period` from a temporal amount.
         *
         *
         * This obtains a period based on the specified amount.
         * A `TemporalAmount` represents an  amount of time, which may be
         * date-based or time-based, which this factory extracts to a `Period`.
         *
         *
         * The conversion loops around the set of units from the amount and uses
         * the [YEARS][ChronoUnit.YEARS], [MONTHS][ChronoUnit.MONTHS]
         * and [DAYS][ChronoUnit.DAYS] units to create a period.
         * If any other units are found then an exception is thrown.
         *
         *
         * If the amount is a `ChronoPeriod` then it must use the ISO chronology.
         *
         * @param amount  the temporal amount to convert, not null
         * @return the equivalent period, not null
         * @throws DateTimeException if unable to convert to a `Period`
         * @throws ArithmeticException if the amount of years, months or days exceeds an int
         */
        fun from(amount: Period): Period {
            if (amount is Period) {
                return amount
            }
//            if (amount is ChronoPeriod) {
//                if (java.time.chrono.IsoChronology.INSTANCE == (amount as java.time.chrono.ChronoPeriod).getChronology() == false) {
//                    throw java.time.DateTimeException("Period requires ISO chronology: " + amount)
//                }
//            }

            var years = 0
            var months = 0
            var days = 0
            for (unit in amount.units) {
                val unitAmount: Long = amount.get(unit)
                if (unit == ChronoUnit.YEARS) {
                    years = Math.toIntExact(unitAmount)
                } else if (unit == ChronoUnit.MONTHS) {
                    months = Math.toIntExact(unitAmount)
                } else if (unit == ChronoUnit.DAYS) {
                    days = Math.toIntExact(unitAmount)
                } else {
                    throw DateTimeException("Unit must be Years, Months or Days, but was $unit")
                }
            }
            return create(years, months, days)
        }

        //-----------------------------------------------------------------------
        /**
         * Obtains a `Period` from a text string such as `PnYnMnD`.
         *
         *
         * This will parse the string produced by `toString()` which is
         * based on the ISO-8601 period formats `PnYnMnD` and `PnW`.
         *
         *
         * The string starts with an optional sign, denoted by the ASCII negative
         * or positive symbol. If negative, the whole period is negated.
         * The ASCII letter "P" is next in upper or lower case.
         * There are then four sections, each consisting of a number and a suffix.
         * At least one of the four sections must be present.
         * The sections have suffixes in ASCII of "Y", "M", "W" and "D" for
         * years, months, weeks and days, accepted in upper or lower case.
         * The suffixes must occur in order.
         * The number part of each section must consist of ASCII digits.
         * The number may be prefixed by the ASCII negative or positive symbol.
         * The number must parse to an `int`.
         *
         *
         * The leading plus/minus sign, and negative values for other units are
         * not part of the ISO-8601 standard. In addition, ISO-8601 does not
         * permit mixing between the `PnYnMnD` and `PnW` formats.
         * Any week-based input is multiplied by 7 and treated as a number of days.
         *
         *
         * For example, the following are valid inputs:
         * <pre>
         * "P2Y"             -- Period.ofYears(2)
         * "P3M"             -- Period.ofMonths(3)
         * "P4W"             -- Period.ofWeeks(4)
         * "P5D"             -- Period.ofDays(5)
         * "P1Y2M3D"         -- Period.of(1, 2, 3)
         * "P1Y2M3W4D"       -- Period.of(1, 2, 25)
         * "P-1Y2M"          -- Period.of(-1, 2, 0)
         * "-P1Y2M"          -- Period.of(-1, -2, 0)
        </pre> *
         *
         * @param text  the text to parse, not null
         * @return the parsed period, not null
         * @throws DateTimeParseException if the text cannot be parsed to a period
         */
        fun parse(text: CharSequence): Period {
            PATTERN.matcher(text.toString())?.let { matcher->
                val negateStr = matcher.groups[0]?.value
                val negate = if(negateStr == "-") -1 else 1
                val years = matcher.groups[0]?.value?.toInt()?:0
                val months = matcher.groups[0]?.value?.toInt()?:0
                val weeks = matcher.groups[0]?.value?.toInt()?:0
                var days = matcher.groups[0]?.value?.toInt()?:0
                if (years >= 0 || months >= 0 || weeks>= 0 || days >= 0) {
                    days = Math.addExact(days, Math.multiplyExact(weeks, 7))
                    return create(years, months, days)
                }

//                val negate = (if (charMatch(text, matcher.start(1), matcher.end(1), '-')) -1 else 1)
//                val yearStart: Int = matcher.start(2)
//                val yearEnd: Int = matcher.end(2)
//                val monthStart: Int = matcher.start(3)
//                val monthEnd: Int = matcher.end(3)
//                val weekStart: Int = matcher.start(4)
//                val weekEnd: Int = matcher.end(4)
//                val dayStart: Int = matcher.start(5)
//                val dayEnd: Int = matcher.end(5)
//                if (yearStart >= 0 || monthStart >= 0 || weekStart >= 0 || dayStart >= 0) {
//                    try {
//                        val years = Companion.parseNumber(text, yearStart, yearEnd, negate)
//                        val months = Companion.parseNumber(text, monthStart, monthEnd, negate)
//                        val weeks = Companion.parseNumber(text, weekStart, weekEnd, negate)
//                        var days = Companion.parseNumber(text, dayStart, dayEnd, negate)
//                        days = Math.addExact(days, Math.multiplyExact(weeks, 7))
//                        return create(years, months, days)
//                    } catch (ex: NumberFormatException) {
//                        throw DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex)
//                    }
//                }
            }
            throw DateTimeParseException("Text cannot be parsed to a Period", text, 0)
        }

        private fun charMatch(text: CharSequence, start: Int, end: Int, c: Char): Boolean {
            return (start >= 0 && end == start + 1 && text[start] == c)
        }

        private fun parseNumber(text: CharSequence, start: Int, end: Int, negate: Int): Int {
            if (start < 0 || end < 0) {
                return 0
            }
            val `val` = text.substring(start, end).toInt()
            try {
                return Math.multiplyExact(`val`, negate)
            } catch (ex: ArithmeticException) {
                throw DateTimeParseException("Text cannot be parsed to a Period", text, 0, ex)
            }
        }

        //-----------------------------------------------------------------------
        /**
         * Obtains a `Period` consisting of the number of years, months,
         * and days between two dates.
         *
         *
         * The start date is included, but the end date is not.
         * The period is calculated by removing complete months, then calculating
         * the remaining number of days, adjusting to ensure that both have the same sign.
         * The number of months is then split into years and months based on a 12 month year.
         * A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
         * For example, from `2010-01-15` to `2011-03-18` is one year, two months and three days.
         *
         *
         * The result of this method can be a negative period if the end is before the start.
         * The negative sign will be the same in each of year, month and day.
         *
         * @param startDateInclusive  the start date, inclusive, not null
         * @param endDateExclusive  the end date, exclusive, not null
         * @return the period between this date and the end date, not null
         * @see ChronoLocalDate.until
         */
        fun between(startDateInclusive: LocalDate, endDateExclusive: LocalDate): Period {
            return startDateInclusive.until(endDateExclusive)
        }

        //-----------------------------------------------------------------------
        /**
         * Creates an instance.
         *
         * @param years  the amount
         * @param months  the amount
         * @param days  the amount
         */
        private fun create(years: Int, months: Int, days: Int): Period {
            if ((years or months or days) == 0) {
                return ZERO
            }
            return Period(years, months, days)
        }

//        @Throws(IOException::class)
//        fun readExternal(`in`: DataInput): Period {
//            val years: Int = `in`.readInt()
//            val months: Int = `in`.readInt()
//            val days: Int = `in`.readInt()
//            return of(years, months, days)
//        }
    }
}

